package com.bytedance.scenedemo.group.inherited;

import com.bytedance.scene.Scene;
import com.bytedance.scene.group.Creator;
import com.bytedance.scene.group.InheritedScene;
import com.bytedance.scenedemo.ResourceTable;
import com.bytedance.scenedemo.utility.ColorUtil;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

/**
 * Created by JiangQi on 8/21/18.
 */
public class InheritedDemo extends InheritedScene {
    Text summary;
    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        ComponentContainer componentContainer = (ComponentContainer) inflater.parse(ResourceTable.Layout_inherited,container,false);
        summary = (Text) componentContainer.findComponentById(ResourceTable.Id_summary);
        return componentContainer;
    }

    @Override
    public void onViewCreated(Component component, Intent intent) {
        super.onViewCreated(component, intent);
       Scene scene = createOrReuse("0", new Creator<Scene>() {
           @Override
           public Scene call() {
               return Child0Scene.newInstance(0);
           }
       });
       Scene scene1 = createOrReuse("1", new Creator<Scene>() {
           @Override
           public Scene call() {
               return Child0Scene.newInstance(1);
           }
       });
       Scene scene2 = createOrReuse("2", new Creator<Scene>() {
           @Override
           public Scene call() {
               return Child0Scene.newInstance(2);
           }
       });
       Scene scene3 = createOrReuse("3", new Creator<Scene>() {
           @Override
           public Scene call() {
               return Child0Scene.newInstance(3);
           }
       });
        if (!isAdded(scene)) add(ResourceTable.Id_block_0, scene, "0");
        if (!isAdded(scene1)) add(ResourceTable.Id_block_1, scene1, "1");
        if (!isAdded(scene2)) add(ResourceTable.Id_block_2, scene2, "2");
        if (!isAdded(scene3)) add(ResourceTable.Id_block_3, scene3, "3");
    }

    static class  Child0Scene extends Scene{
        private Child0Scene(){
        }
        public static Child0Scene newInstance(int index){
            Child0Scene scene = new Child0Scene();
            IntentParams intentParams = new IntentParams();
            intentParams.setParam("index",index);
            scene.setArguments(intentParams);
            return scene;
        }
        @Override
        public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
            Component component = new Component(getAbility());
            return component;
        }

        @Override
        public void onViewCreated(Component component, Intent intent) {
            super.onViewCreated(component, intent);
            int index = 0;
            if(getArguments()!=null){
                 index = (int) getArguments().getParam("index");
            }
            getView().setBackground(ColorUtil.getMaterialColor(index));
            int finalIndex = index;
            getView().setClickedListener(component1 -> {
                InheritedDemo inheritedScene = getScope().getService(InheritedDemo.class);
                inheritedScene.summary.setText("Child Scene"+ finalIndex);
            });

        }
    }
}
